package com.zer0.tga.level;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import com.zer0.tga.Game;
import com.zer0.tga.level.Room.RoomType;
import com.zer0.tga.math.RandNum;
import com.zer0.tga.level.Room;


public class RoomGenerator {
	
	public class RoomInfo
	{
		public int[] RoomInterior;
		public int roomID;
		public ArrayList<String> MobPool = new ArrayList<String>();
	}
	
	private static final int ROOM_FILE_COUNT = 8;
	
	private static ArrayList<RoomInfo> roomList;

	private static RoomGenerator rg;
	
	private RoomGenerator()
	{
		roomList = new ArrayList<RoomInfo>();
		importFromFile();
	}
	
	public static RoomGenerator getInstance()
	{
		if(rg == null)
			rg = new RoomGenerator();
		return rg;
	}
	
/*
	public static void importFromFile(){
		
		String filestr = "res/rooms2.txt";
		BufferedReader reader = null;
		try {

			if(Game.class.getResource("Game.class").toString().startsWith("jar"))
			{
				reader = new BufferedReader(new InputStreamReader(Game.class.getClass().getResourceAsStream("/" + filestr)));
			}
			else
			{
				File roomFile = new File(filestr);
				reader = new BufferedReader(new FileReader(roomFile));
			}
		String line = null;	
	    while ((line=reader.readLine()) != null) 
	    {
	    	if(line.length() == (Room.ROOM_X * Room.ROOM_Y))
	    	{
	    		int[] newRoomTemp = new int[Room.ROOM_X * Room.ROOM_Y];
	    		for(int i=0;i<line.length()-1;i++)
	    		{
	    			newRoomTemp[i] = Integer.parseInt(line.substring(i, i+1));
	    		}
	    		//add the last one
	    		newRoomTemp[line.length()-1] = Integer.parseInt(line.substring(line.length()-1));
	    		
	    		//add to the list
	    		roomList.add(newRoomTemp);
	    	}
	    }
	    reader.close();
	
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	*/
	
	public void importFromFile()
	{

		String filestr = "res/room_";
		String filestr2 = ".txt";
		
		for(int fileNo=1; fileNo<=ROOM_FILE_COUNT; fileNo++)
		{
			RoomInfo newRoom = new RoomInfo();
			BufferedReader reader = null;
			try {

				if(Game.class.getResource("Game.class").toString().startsWith("jar"))
				{
					reader = new BufferedReader(new InputStreamReader(Game.class.getClass().getResourceAsStream("/" + filestr+fileNo+filestr2)));
				}
				else
				{
					File roomFile = new File(filestr+fileNo+filestr2);
					reader = new BufferedReader(new FileReader(roomFile));
				}
				
			String line = null;
			int[] newRoomTemp = new int[Room.ROOM_X * Room.ROOM_Y];
			//first we read the room interior
		    for(int j = 0; j<Room.ROOM_Y ; j++)
		    {
		    	line=reader.readLine();
		    		    
		    	if(line.length() == Room.ROOM_X)
		    	{
		    		for(int i=0;i<Room.ROOM_X-1;i++)
		    		{
		    			newRoomTemp[j*Room.ROOM_X + i] = Integer.parseInt(line.substring(i, i+1));
		    		}
		    		//add the last one
		    		newRoomTemp[(j+1)*Room.ROOM_X - 1] = Integer.parseInt(line.substring(line.length()-1));
		    	}
		    }
		    
		    line=reader.readLine(); //read an empty line
		    
		    while((line=reader.readLine()) != null)
		    {
		    	newRoom.MobPool.add(line);
		    }
		    
		    newRoom.RoomInterior = newRoomTemp;
		    newRoom.roomID = fileNo;
		  //add to the list
    		roomList.add(newRoom);
		    
		    reader.close();
		
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
	
	public RoomInfo getRandomRoom(Level level, RoomType rt)
	{
		/*RoomInfo room = roomList.get(RandNum.getRandNum(0, roomList.size()));
		
		if(rt==RoomType.NORMAL_ROOM)
			room = roomList.get(RandNum.getRandNum(0, roomList.size()));
		else if(rt == RoomType.START_ROOM || rt == RoomType.BOSS_ROOM || rt == RoomType.SHOP_ROOM)
			room = roomList.get(0);
		else
			room = roomList.get(RandNum.getRandNum(0, roomList.size()));
			
		return room;*/
		
		if(rt == RoomType.START_ROOM || rt == RoomType.BOSS_ROOM || rt == RoomType.SHOP_ROOM)
			return roomList.get(0);
		else
			return roomList.get(RandNum.getRandNum(0, roomList.size()));

	}
	
	
}
